<?php

/**
 * MVM_MALL 网上商店系统  疯狂优惠活动管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-20 $
 * $Id: privilege.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if($action=='list'){
    require_once MVMMALL_ROOT.'./include/pager.class.php';
    $total_count = $db->counter("{$tablepre}activity","where act_type='3'");
    $page        = $page ? (int)$page:1;
    $list_num    = 10;
    $rowset      = new Pager($total_count,$list_num,$page);
    $from_record = $rowset->_offset();
    $sql         = "SELECT a.*,b.uid,b.goods_file1,b.goods_name FROM `{$tablepre}activity` a LEFT JOIN `{$tablepre}goods_table` b ON a.goods_id=b.uid WHERE act_type='3' LIMIT $from_record, $list_num";
    $result = $db->query($sql);
    while ($rt = $db->fetch_array($result)) {
        $rt['start_time']  = date($mm_date_format, $rt['start_time']);
        $rt['end_time']    = date($mm_date_format, $rt['end_time']);
        $rt['photo']       = $rt['goods_file1'];
        $rt['edit']        = "admincp.php?module=$module&action=edit&uid=$rt[act_id]";
        $rt['del']         = "admincp.php?module=$module&action=del&per=ajax&uid=$rt[act_id]";
        $rt['url']         = "privilege.php?action=view&id=$rt[act_id]";
        $rs                = array_merge($rt, unserialize($rt['act_config']));
        $priv_rt[]         = $rs;
    }
    $page_list = $rowset->link("admincp.php?module=$module&action=$action&page=");
    require_once template('privilege');
    footer();
} elseif ($action=='add'){
    if($setp==1 && $_POST){
        $act_title  = dhtmlchars($act_title);
        $act_desc   = dhtmlchars($act_desc);
        $goods_id   = intval($goods_id);
        $start_time = time_format($start_time);
        $end_time   = time_format($end_time);
        $act_config = serialize(array('start_price'=>floatval($start_price),
                                       'end_price' =>floatval($end_price),
                                       'buy_price' =>floatval($buy_price),
                                       ));
        $query = " INSERT INTO `{$tablepre}activity` SET
                   act_title        = '$act_title',
                   act_desc         = '$act_desc',
                   act_type         = '3',
                   goods_id         = '$goods_id',
                   start_time       = '$start_time',
                   end_time         = '$end_time',
                   act_config       = '$act_config'
                   ";
        $db->query($query);
        admin_msg('success','admincp.php?module=privilege&action=list');
    } else {
        $start_time = date('Y-m-d',$m_now_time);
        require_once template('privilege_add');
        footer();
    }
} elseif ($action == 'edit' && is_numeric($uid)) {
    if($setp==1&&$_POST&&$uid){
        $act_title  = dhtmlchars($act_title);
        $act_desc   = dhtmlchars($act_desc);
        $goods_id   = intval($goods_id);
        $start_time = time_format($start_time);
        $end_time   = time_format($end_time);
        $act_config = serialize(array('start_price'=>floatval($start_price),
                                       'end_price' =>floatval($end_price),
                                       'buy_price' =>floatval($buy_price),
                                       ));
        $query = "UPDATE `{$tablepre}activity` SET
                   act_title        = '$act_title',
                   act_desc         = '$act_desc',
                   act_type         = '3',
                   goods_id         = '$goods_id',
                   start_time       = '$start_time',
                   end_time         = '$end_time',
                   act_config       = '$act_config'
                   WHERE act_id='$uid'";
        $db->query($query);
        admin_msg('success','admincp.php?module=privilege&action=list');
    } else {
        $rt    = $db->get_one("SELECT a.*,b.uid,b.goods_file1,b.goods_name FROM `{$tablepre}activity` a LEFT JOIN `{$tablepre}goods_table` b ON a.goods_id=b.uid WHERE a.act_type='3' AND a.act_id='$uid'");
        $cf_rt = unserialize($rt['act_config']);
        unset($rt['act_config']);
        @extract($rt,EXTR_OVERWRITE);
        @extract($cf_rt,EXTR_OVERWRITE);
        $start_time = date('Y-m-d',$start_time);
        $end_time   = date('Y-m-d',$end_time);
        require_once template('privilege_add');
        footer();
    }
} else if($action=='del'){
    if (is_numeric($uid)) {
        $db->query("DELETE FROM `{$tablepre}auction_table` WHERE  act_id='$uid'");
        $db->query("DELETE FROM `{$tablepre}activity` WHERE act_type='3' AND act_id='$uid'");
    }elseif (is_array($uid_check)){
        for($i=0;$i<count($uid_check);$i++) {
            $uid = (int)$uid_check[$i];
            $db->query("DELETE FROM `{$tablepre}auction_table` WHERE  act_id='$uid'");
            $db->query("DELETE FROM `{$tablepre}activity` WHERE act_type='3' AND act_id='$uid'");
        }
    }else {
        admin_msg('pass_worng');
    }
    $per!='ajax' && admin_msg('success',dreferer());
}  else {
    admin_msg('pass_worng');
}
